import wepy from 'wepy'
import { config } from './config'

const header = {
  'content-type': 'application/json',
  'X-Requested-With': 'XMLHttpRequest'
}

const api = {
  request: (url, data, method = 'GET', options = {loading: true}) => {
    return new Promise((resolve, reject) => {
      if (!url.startsWith('https:') && !url.startsWith('http:')) {
        url = config.host + url
        try {
          let token = wepy.getStorageSync('access_token')
          if (token) {
            // Do something with return value
            header.Authorization = `Bearer ${token}`
          }
          header.app = 0
        } catch (e) {
          // Do something when catch error
        }
      }
      if (options.loading) {
        wepy.showLoading({
          title: options.title ? options.title : '加载中...'
        })
      }
      wepy.request({
        url: url,
        header: header,
        method: method,
        data: data,
        success: (res) => {
          let accessToken = res.header.Authorization
          if (accessToken) {
            accessToken = accessToken.replace('Bearer ', '')
            wepy.setStorageSync('access_token', accessToken)
          }
          if (res.statusCode === 200) {
            resolve(res.data)
            if (options.loading) {
              wepy.hideLoading()
            }
          } else if (res.statusCode === 401) {
            // wx.request({
            //     url:`${config.host}api/refresh-token`,
            //     header:header,
            //     method:'GET',
            //     success:(res)=>{
            //         if (res.statusCode === 200) {
            //             wx.setStorageSync('access_token',res.data.data.access_token)
            //         }else {
            wepy.removeStorageSync('access_token')
            reject(res)
            wepy.navigateTo({
              url: '/pages/user/login/login'
            })
            wepy.hideLoading()
            // }
            // }
            // })
          } else {
            if (options.loading) {
              wepy.hideLoading()
            }
            reject(res.data)
          }
        }
      })
    })
  },
  upload: (url, filePath, name, formData = {}, options = {loading: true}) => {
    return new Promise((resolve, reject) => {
      if (!url.startsWith('https:') && !url.startsWith('http:')) {
        url = config.host + url
        try {
          let token = wepy.getStorageSync('access_token')
          if (token) {
            // Do something with return value
            url = url + '?token=' + token
          }
        } catch (e) {
          // Do something when catch error
        }
      }
      if (options.loading) {
        wepy.showLoading({
          title: options.title ? options.title : '上传中...'
        })
      }
      wepy.uploadFile({
        url: url,
        filePath: filePath,
        name: name,
        formData: formData,
        success: res => {
          if (res.statusCode === 200) {
            resolve(JSON.parse(res.data))
            if (options.loading) {
              wepy.hideLoading()
            }
          } else if (res.statusCode === 401) {
            reject(JSON.parse(res.data))
            if (options.loading) {
              wepy.hideLoading()
            }
            wepy.showToast({
              title: '登录已过期',
              duration: 1500
            })
            wepy.navigateTo({
              url: '/pages/user/login/login'
            })
            wepy.removeStorageSync('access_token')
          }
        }
      })
    })
  }
}

export default api
